<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Websocket Client Test</title>
</head>
<body>
    <form action="" onsubmit="return false;">
        <textarea name="message" rows="10" cols="100"></textarea>
        <br>
        <input type="button" value="send" value="发送websocket请求消息" onclick="send(this.form.message.value);"/>
        <hr color="blue">
        <h3>服务器返回信息</h3>
        <div id="responseText" style="display: block;overflow-y: auto;width: 2000px;height: 600px;border: solid 1px #000;"></div>
    </form>
</body>

<script type="text/javascript">
    var socket;
    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }
    if (window.WebSocket) {
        // socket = new WebSocket("wss://iot.bamtechnology.cn/websocket");
        socket = new WebSocket("ws://127.0.0.1:10013/websocket");
        socket.onmessage = function (event) {
            var ta = document.getElementById('responseText');
            ta.innerHTML = event.data.replace(/\n/g, "<br />");
            //ta.innerHTML = ta.innerHTML + event.data.replace(/\n/g, "<br />");
            ta.scrollTop = ta.scrollHeight;
        };
        socket.onopen = function (event) {
            var ta = document.getElementById('responseText');
            ta.innerHTML = "打开websocket服务正常";
        }
        socket.onclose = function (event) {
            var ta = document.getElementById('responseText');
            ta.innerHTML = "";
            ta.innerHTML = "websocket关闭";
        }
    } else {
        alert("对不起，您的浏览器不支持WebSocket.");
    }

    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
            alert("发送成功");
        } else {
            alert("WebSocket 连接创建失败.");
        }
    }
</script>
</html>